package com.chenyun.common.vo;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * @descriptions: 结果返回类
 * @author:
 * @date: 2020/4/26 17:21
 * @version: 1.0
 */
@Data
@AllArgsConstructor
public class AjaxResult<T>   implements Serializable  {
    //是否成功
    private boolean success;
    //返回码
    private Integer code;
    //返回信息
    private String message;
    //返回数据
    private T data;
    public AjaxResult(){}

    public AjaxResult(ResultCode code) {
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
    }

    public AjaxResult(ResultCode code, T data) {
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
        this.data = data;
    }
    public AjaxResult(ResultCode code, String msg) {
        this.success = code.success;
        this.code = code.code;
        this.message = msg;
    }
    public AjaxResult(Integer code, String message, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }
    public AjaxResult(Integer code, String message, boolean success,T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data=data;
    }
    public static <T> AjaxResult<T> SUCCESS(T data){
        return new AjaxResult<T>(ResultCode.SUCCESS,data);
    }
    public static <T> AjaxResult<T> SUCCESS(){
        return new AjaxResult<T>(ResultCode.SUCCESS);
    }
    public static <T> AjaxResult<T> SUCCESS(String msg,T data){
        return new AjaxResult<T>(ResultCode.SUCCESS.code,msg,true,data);
    }


    public static AjaxResult ERROR(){
        return new AjaxResult(ResultCode.SERVER_ERROR);
    }
    public static AjaxResult ERROR(String msg){
        return new AjaxResult(ResultCode.SERVER_ERROR,msg);
    }
    public static AjaxResult FAIL(){
        return new AjaxResult(ResultCode.FAIL);
    }
    public static  AjaxResult buildServiceResponse(ServiceResponse response){
       if (response.getSuccess()){
           return AjaxResult.SUCCESS(response.getData());
       }else {
           return new AjaxResult(ResultCode.FAIL,response.getMsg());
       }

    }

    public static <T> AjaxResult<T> FAIL(String msg,T data){
        return new AjaxResult<T>(ResultCode.FAIL.code,msg,false,data);
    }
    public static <T> AjaxResult<T> FAIL(String msg){
        return new AjaxResult<T>(ResultCode.FAIL,msg);
    }

    @Override
    public String toString() {
        return "AjaxResult{" +
                "success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
